import { director, ISchedulable, Scheduler } from "cc";
import { IMgr } from "./IMgr";


export class TimerMgr implements IMgr{

    init() {
    }
    finit() {
    }

    setInterval(handler: (dt?: number) => void, target: ISchedulable, interval: number, ...args: any[]): void {

        Scheduler.enableForTarget(target);

        if (director.getScheduler().isScheduled(handler, target)) {
            return;
        }

        return director.getScheduler().schedule(handler, target, interval)
    }

    clearInterval(callback: Function, target: ISchedulable): void {
        director.getScheduler().unschedule(callback, target);
    }

    setTimeOut(handler: (dt?: number) => void, target: ISchedulable, timeOut: number, ...args: any[]): void {

        Scheduler.enableForTarget(target);

        if (director.getScheduler().isScheduled(handler, target)) {
            return;
        }

        return director.getScheduler().schedule(handler, target, timeOut, 0, 0, false);

    }

    clearTimeOut(callback: Function, target: ISchedulable): void {
        director.getScheduler().unschedule(callback, target);
    }
}